package zero.tech.games.logic.process;

import org.apache.commons.lang3.StringUtils;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.Resp;
import zero.tech.core.work.QueueWork;
import zero.tech.core.work.WorkManager;
import zero.tech.core.work.WorkQueue;
import zero.tech.games.common.Code;
import zero.tech.games.common.UserLastRoomUtil;
import zero.tech.games.common.data.UserLastRoom;
import zero.tech.games.logic.common.CacheRoom;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.util.LogicUtil;
import zero.tech.wsnet.common.ReqProcess;
import zero.tech.wsnet.controller.LogicController;
import zero.tech.wsnet.data.Message;
import zero.tech.wsnet.net.WebClient;

public abstract class ReqRoomProcess extends ReqProcess{

	@Override
	public void process(WebClient webClient, Resp param) {
		LogicController controller = (LogicController) webClient.getController();
		if (controller == null) {
			webClient.sendMessage(Message.build(Code.OK, Resp.build(Code.NOT_IN_ROOM, "不在房间内")));
			return;
		}
		String roomId = controller.getRoomId();
		int uid=controller.getId();
		if (StringUtils.isBlank(roomId)) {
			webClient.sendMessage(Message.build(Code.OK, Resp.build(Code.NOT_IN_ROOM, "不在房间内")));
			return;
		}
		final Room room = CacheRoom.getCache().getRooms().get(roomId);
		if (room == null) {
			UserLastRoomUtil userLastRoomUtil =ZeroContext.getInstance(UserLastRoomUtil.class);
			UserLastRoom userLastRoom=userLastRoomUtil.getUserLastRoom(String.valueOf(uid));
			if(userLastRoom!=null) {
				String club_id=userLastRoom.getClub_id();
				LogicUtil.clearRoom(uid, roomId, club_id);
			}
			webClient.sendMessage(Message.build(Code.OK, Resp.build(Code.NOT_IN_ROOM, "不在房间内")));
			return;
		}
		WorkManager.getManager().submit(new QueueWork() {
			
			@Override
			public void run() {
				process(room, controller, param);
			}
			
			@Override
			public void init(Object... objs) throws Exception {
				
			}
			
			@Override
			public WorkQueue getWorkQueue() {
				return new WorkQueue() {
					
					@Override
					public long getId() {
						return room.getQueue_id();
					}
				};
			}
		});
		
	}
	public abstract void process(Room room, LogicController controller, Resp param);

}
